package com.thunder.soft.ui.fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.thunder.soft.R;
import com.thunder.soft.bean.SystemBean;
import com.thunder.soft.common.base.BaseFragment;
import com.thunder.soft.common.indicatorview.IndicatorItem;
import com.thunder.soft.common.util.ResourceUtil;
import com.thunder.soft.common.util.ViewModelProviderHelper;
import com.thunder.soft.databinding.FragmentSystemBinding;
import com.thunder.soft.ui.adapter.SystemTwoAdapter;
import com.thunder.soft.vm.SystemViewModel;

import java.util.ArrayList;
import java.util.List;

public class SystemFragment extends BaseFragment<FragmentSystemBinding> {

    private SystemViewModel viewModel;
    private LayoutInflater layoutInflater = null;
    List<Integer> colorList = new ArrayList<>();
    public SystemFragment() {
    }

    @Override
    protected void initData() {
        viewModel = ViewModelProviderHelper.buildViewModel(this, SystemViewModel.class);
        getLifecycle().addObserver(viewModel);

        viewModel.getErrorMsg().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Toast.makeText(getContext(), s, Toast.LENGTH_SHORT).show();
            }
        });

        binding.indicatorScrollView.bindIndicatorView(binding.indicatorView);
        for (int i = 0; i < 19; i++) {
            int resId = ResourceUtil.getResId("in" + (i + 1), R.color.class);
            colorList.add(resId);
        }

        viewModel.getLiveData().observe(this, new Observer<List<SystemBean>>() {
            @Override
            public void onChanged(List<SystemBean> systemBeans) {
                setData(systemBeans);
            }
        });

    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_system;
    }


    private View findItem() {
        if (layoutInflater == null)
            layoutInflater = LayoutInflater.from(getContext());
        return (View) layoutInflater.inflate(R.layout.adapter_system, null, false);
    }

    protected void setData(List<SystemBean> data) {
        binding.llParent.removeAllViews();
        binding.indicatorView.removeAllViews();
        for (int i = 0; i < data.size(); i++) {
            String name = data.get(i).getName();
            String firstName = TextUtils.isEmpty(name) ? "" : name.substring(0, 1);
            View view = findItem();

            TextView textView = view.findViewById(R.id.tv_system);
            textView.setText(name);

            RecyclerView recyclerView = view.findViewById(R.id.system_rv_in);
            SystemTwoAdapter systemTwoAdapter = new SystemTwoAdapter();
            FlexboxLayoutManager flexboxLayoutManager=new FlexboxLayoutManager(getContext(), FlexDirection.ROW);
            flexboxLayoutManager.setJustifyContent(JustifyContent.FLEX_START);
            flexboxLayoutManager.setAlignItems(AlignItems.CENTER);

            recyclerView.setLayoutManager(flexboxLayoutManager);
            recyclerView.setAdapter(systemTwoAdapter);

            systemTwoAdapter.addList(data.get(i).getChildren());

            binding.llParent.addView(view);
            int j = i % colorList.size();
            binding.indicatorView.addIndicatorItem(new IndicatorItem.Builder(view).setItemText(firstName).setItemColorResource(colorList.get(j)).setItemIconResource(R.mipmap.ic_uncollect).build());
        }
    }
}